<?php
/**
 * AttributeHandle
 *
 * @Description 描述
 * @Author look
 * @CreateTime 2022/2/23
 */

namespace app\common\handle;

use app\common\Attributes\Auth;
use app\common\Attributes\Method;
use think\Request;

/**
 * Class AttributeHandle
 */
class AttributeHandle {

    private array $bind = [
        Method::class,
        Auth::class
    ];

    public function handle(Request $request, string $baseClass) {
        $obj = new \ReflectionClass($baseClass);
        $actionReflection = $obj->getMethod($request->action());

        foreach ($this->bind as $bind) {
            $actionAttributeReflections = $actionReflection->getAttributes($bind);
            if ($actionAttributeReflections) {
                array_map(function ($actionAttributeReflection) use ($request)  {
                    $actionAttributeReflection->newInstance()->exec($request);
                }, $actionAttributeReflections);
            }
        }
    }

}